// 用户权限管理 - 用户菜单权限接口
const Router = require("koa-router");
const router = new Router();

// 用户菜单权限数据结构
const userMenulist = [
  {
    name: "数据看板",
    url: "/dashboard",
    icon: "DataLine",
  },
  {
    name: "充电站管理",
    url: "/chargingstation",
    icon: "Lightning",
    children: [
      {
        name: "充电站监控",
        url: "/chargingstation/monitor",
        icon: "VideoCamera",
      },
      {
        name: "充电桩管理",
        url: "/chargingstation/fault",
        icon: "Warning",
      },
    ],
  },
  {
    name: "电子地图",
    url: "/map",
    icon: "MapLocation",
  },
  {
    name: "报警管理",
    url: "/alarm",
    icon: "Phone",
  },
  {
    name: "会员卡管理",
    url: "/equipment",
    icon: "Magnet",
  },
  {
    name: "个人中心",
    url: "/personal",
    icon: "User",
  },
];

// 经理菜单权限（比用户多一些权限）
const managerMenulist = [
  {
    name: "数据看板",
    url: "/dashboard",
    icon: "DataLine",
  },
  {
    name: "充电站管理",
    url: "/chargingstation",
    icon: "Lightning",
    children: [
      {
        name: "充电站监控",
        url: "/chargingstation/monitor",
        icon: "VideoCamera",
      },
      {
        name: "充电桩管理",
        url: "/chargingstation/fault",
        icon: "Warning",
      },
      {
        name: "充电站统计",
        url: "/chargingstation/statistics",
        icon: "TrendCharts",
      },
    ],
  },
  {
    name: "电子地图",
    url: "/map",
    icon: "MapLocation",
  },
  {
    name: "报警管理",
    url: "/alarm",
    icon: "Phone",
  },
  {
    name: "会员卡管理",
    url: "/equipment",
    icon: "Magnet",
  },
  {
    name: "系统设置",
    url: "/system",
    icon: "Setting",
    children: [
      {
        name: "用户管理",
        url: "/system/user",
        icon: "User",
      },
    ],
  },
  {
    name: "个人中心",
    url: "/personal",
    icon: "User",
  },
];

// 管理员菜单权限（拥有所有权限）
const adminMenulist = [
  {
    name: "数据看板",
    url: "/dashboard",
    icon: "DataLine",
  },
  {
    name: "充电站管理",
    url: "/chargingstation",
    icon: "Lightning",
    children: [
      {
        name: "充电站监控",
        url: "/chargingstation/monitor",
        icon: "VideoCamera",
      },
      {
        name: "充电桩管理",
        url: "/chargingstation/fault",
        icon: "Warning",
      },
      {
        name: "充电站统计",
        url: "/chargingstation/statistics",
        icon: "TrendCharts",
      },
      {
        name: "充电站设置",
        url: "/chargingstation/settings",
        icon: "Setting",
      },
    ],
  },
  {
    name: "电子地图",
    url: "/map",
    icon: "MapLocation",
  },
  {
    name: "报警管理",
    url: "/alarm",
    icon: "Phone",
  },
  {
    name: "会员卡管理",
    url: "/equipment",
    icon: "Magnet",
  },
  {
    name: "招商管理",
    url: "/business",
    icon: "Briefcase",
  },
  {
    name: "权限管理",
    url: "/permission",
    icon: "Lock",
  },
  {
    name: "系统设置",
    url: "/system",
    icon: "Setting",
    children: [
      {
        name: "用户管理",
        url: "/system/user",
        icon: "User",
      },
      {
        name: "角色管理",
        url: "/system/role",
        icon: "UserFilled",
      },
      {
        name: "系统配置",
        url: "/system/config",
        icon: "Tools",
      },
    ],
  },
  {
    name: "个人中心",
    url: "/personal",
    icon: "User",
  },
];

/**
 * 获取当前用户权限接口
 * 路径：POST /userAuth/userAuth
 *
 * 请求参数：
 * {
 *   pageAuthority: "user" // 用户权限类型：user(用户), manager(经理), admin(管理员)
 * }
 */
router.post("/permission/userAuth", (ctx) => {
  try {
    // 解析请求参数
    const requestBody = ctx.request.body;
    const { pageAuthority } = requestBody;

    console.log("用户权限接口收到参数:", { pageAuthority });

    if (!pageAuthority) {
      ctx.body = {
        code: 400,
        message: "缺少必要的参数: pageAuthority",
        data: null,
      };
      return;
    }

    let menuList;
    let btnPermissions;

    // 根据权限类型返回不同的菜单和按钮权限
    switch (pageAuthority) {
      case "user":
        menuList = userMenulist;
        btnPermissions = ["add"];
        break;
      case "manager":
        menuList = managerMenulist;
        btnPermissions = ["add", "edit"];
        break;
      case "admin":
        menuList = adminMenulist;
        btnPermissions = ["add", "edit", "all", "delete"];
        break;
      default:
        // 默认返回用户权限
        menuList = userMenulist;
        btnPermissions = ["add"];
    }

    console.log(`返回权限数据: ${pageAuthority} 权限`);

    // 返回响应
    ctx.body = {
      code: 200,
      message: "操作成功",
      data: {
        list: menuList,
        btn: btnPermissions,
      },
    };
  } catch (error) {
    console.error("用户权限接口出错:", error);
    ctx.body = {
      code: 500,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});


/**
 * 获取所有权限类型接口
 * 路径：GET /userAuth/authTypes
 *
 * 说明：此接口返回系统中所有可用的权限类型
 */
router.get("/permission/authTypes", (ctx) => {
  try {
    console.log("获取权限类型接口被调用");

    // 返回权限类型列表
    ctx.body = {
      code: 200,
      message: "操作成功",
      data: {
        authTypes: [
          { value: "user", label: "普通用户", description: "基础功能权限" },
          { value: "manager", label: "经理", description: "管理功能权限" },
          { value: "admin", label: "管理员", description: "系统所有权限" },
        ],
      },
    };
  } catch (error) {
    console.error("获取权限类型接口出错:", error);
    ctx.body = {
      code: 500,
      message: `服务器内部错误: ${error.message}`,
      data: null,
    };
  }
});

// 导出路由
module.exports = router;
